<?php
 class ControllerCommonRegister extends Controller {
 	private $error = array();
 	public function index() {
 		$this->document->setTitle("register");
 		$this->load->model('user/user');
 		if(($this->request->server['REQUEST_METHOD'] == 'POST')) {
 			$info['firstname'] = 'song';
 			$info['lastname'] = 'guannan';
 			$info['email'] = $this->request->post['email'];
 			$info['username'] = $this->request->post['username'];
 			$info['password'] = $this->request->post['password'];
 			$info['status'] = '0';
 			$user_id = $this->model_user_user->addUser($info);
 			if(!isset($user_id)){
 				$this->error['user_id'] = "注册失败，请重试！";
 			}
 			header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($this->error,0));
 		}
 		$data['action'] = $this->url->link('common/register', '', true);
 		$data['loginurl'] = $this->url->link('common/login', '', true);
 		$data['registervel'] = $this->url->link('common/register/validate', '',true);
 		$data['resendemail'] = $this->url->link('common/register/resendemail','',true);
 		$data['header'] = $this->load->controller('common/headerout');
 		$data['footerout'] = $this->load->controller('common/footerout');
 		$data['footer'] = $this->load->controller('common/footer');
 		$this->response->setOutput($this->load->view('common/register', $data));
 	}
 	public function validate() {
 		$this->load->model('user/user');
 		if ($this->model_user_user->getTotalUsers($this->request->post['username'])) {
 			$this->error['username'] = "该用户名已注册";
 		}
 		if ($this->model_user_user->getTotalUsersByEmail($this->request->post['email'])) {
 			$this->error['email'] = "该邮箱已注册";
 		}
 		header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($this->error,0));
 	}
 	public function resendemail() {
 		$this->load->model('user/user');
 		$data = $this->model_user_user->getUseridByEmail($this->request->post['email']); 
 		$customer_id = $data['user_id'];
 		$status = $data['status'];
 		if($customer_id){
 			if($status === "0"){
 				//重新发送激活邮件开始
				$url = $this->url->link('common/login', '', true);
				$url .= "&userid=".$customer_id;
				$this->load->library("email");
				$smtpemailto = utf8_strtolower($this->request->post['email']);//发送给谁
				$mailtitle = "激活邮件请勿回复";//邮件主题
				$mailcontent = "请<a href='".$url."'>点击链接</a>以激活您的账号！";//邮件内容

				$smtp = new email($smtpemailto,$mailtitle,$mailcontent);
				$state = $smtp->sendemail();
				header('Content-Type:application/json; charset=utf-8');
	            exit(json_encode(1,0));
 			}elseif($status === "1"){
 				header('Content-Type:application/json; charset=utf-8');
                exit(json_encode(3,0));
 			}
				
		}else{
			header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(2,0));
		}
 	}
 }